<?php

class Kd_Db_Factory {

    private static $_drivers = array();
//    private static $_transactions = array();

    public static function getDriver($connectionName = NULL) {
        $connectionName = is_null($connectionName) ? Kd_Db_Config::getDefaultConnection() : $connectionName;
        if (!isset(self::$_drivers[$connectionName])) {
            $setting = Kd_Db_Config::getConnectionSetting($connectionName);
            $driverClass = $setting['driver'];
            $result = new $driverClass();
            $result->setServer($setting['server'])
                    ->setDatabase($setting['database'])
                    ->setUsername($setting['username'])
                    ->setPassword($setting['password'])
                    ->connect();
            self::$_drivers[$connectionName] = $result;
        }
        return self::$_drivers[$connectionName];
    }

    public static function getTransaction($connectionName = NULL) {
        $connectionName = is_null($connectionName) ? Kd_Db_Config::getDefaultConnection() : $connectionName;
        return new Kd_Db_Transaction(self::getDriver($connectionName));
    }
}

